package cn.dengta.webapp.trade.entity;

import cn.dengta.common.model.BizFlag;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.base.entity.I18nData;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 礼物（赠送）记录
 *
 * @author lgy
 * @since 2022-02-28
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("tr_gift_log")
public class GiftLog extends BizEntity implements BizFlag.WritableBizFlag {

	/** 标识：发送方电视墙匿名 */
	public static final long BIZ_FLAG_SEND_TV_ANONYMOUS = 1L;
	/** 标识：接收方电视墙匿名 */
	public static final long BIZ_FLAG_TO_TV_ANONYMOUS = 1L << 2;
	/** 标识：发送方飘窗匿名 */
	public static final long BIZ_FLAG_SEND_GIFT_ANONYMOUS = 1L << 3;
	/** 标识：接收方飘窗匿名 */
	public static final long BIZ_FLAG_TO_GIFT_ANONYMOUS = 1L << 4;

	/** 类型（1-普通礼物 2-锁屏礼物） {@link cn.dengta.webapp.im.dto.TvWallConfig.TvWallType} */
	private Integer type;
	/** 用户id */
	private Long sendUserId;
	/** 用户昵称 */
	private String sendUserNickname;
	/** 用户头像 */
	private String sendUserAvatar;
	/** 收礼物用户id */
	private Long toUserId;
	/** 收礼物用户昵称 */
	private String toUserNickname;
	/** 收礼物用户头像 */
	private String toUserAvatar;
	/** 礼物id */
	private Long giftId;
	/** 礼物名称 */
	private String giftName;
	/** 礼物icon */
	private String giftIcon;
	/** 礼物数量 */
	private Integer giftNum;
	/** 钻石（总价） */
	private Integer totalCoin;
	/** 电视墙停留时间 */
	private Integer stayTime;
	/** 类型：0=一对一；1=群聊；2=直播；*/
	private Integer source;
	/** 业务标识 */
	private long bizFlag;
	/** 群id */
	private Long groupId;
	/** 直播标识id */
	private Long liveId;
	/** 通话 目前只有速配会写入 */
	private String channelName;
	/** Ai标识 */
	private Long aiId;

	/** 送礼给你的人数 */
	private transient Integer giftSendNumber;

	/** 推送时候赋值gift name */
	private transient I18nData<String> i18nNameMap;
	private transient String version;

	public String getSendShortName() {
		return StringUtil.limitChars(getSendUserNickname(), 5, "...");
	}

	public String getToShortName() {
		return StringUtil.limitChars(getToUserNickname(), 5, "...");
	}

	/** status状态，0=待播放，1=已播放 */

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public void setBizFlag(long bizFlag) {
		this.bizFlag = bizFlag;
	}

	public boolean isAnonymous(boolean fromOrTo) {
		return "*".equals(fromOrTo ? getSendUserNickname() : getToUserNickname());
	}

	public String getI18nName(Language language) {
		return X.map(i18nNameMap, m -> m.get(language));
	}

	public boolean isToAnonymous() {
		return "*".equals(getToUserNickname());
	}

	public void anonymous(boolean fromOrTo, String avatar) {
		if (fromOrTo) {
			setSendUserNickname("*");
			setSendUserAvatar(StringUtil.toString(avatar));
		} else {
			setToUserNickname("*");
			setToUserAvatar(StringUtil.toString(avatar));
		}
	}

	public static final String TO_USER_ID = "to_user_id";
	public static final String SOURCE = "source";
	public static final String CHANNEL_NAME = "channel_name";

}
